/**
 * Copyright 2013, Haruki Hasegawa
 * <p>
 * Licensed under the MIT license:
 * http://creativecommons.org/licenses/MIT/
 */

package com.pheelicks.app.utils;

import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.audio.AudioRenderer;

public class TunnelPlayerWorkaround {
    private static HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x000001, "TunnelPlayerWorkaround");

    private static final String SYSTEM_PROP_TUNNEL_DECODE_ENABLED = "tunnel.decode";

    private TunnelPlayerWorkaround() {
    }

    /**
     * Obtain "tunnel.decode" system property value
     *
     * @param context Context
     * @return Whether tunnel player is enabled
     */
    public static boolean isTunnelDecodeEnabled(Context context) {
        return SystemPropertiesProxy.getBoolean(
            context, SYSTEM_PROP_TUNNEL_DECODE_ENABLED, false);
    }

    /**
     * Create silent MediaPlayer instance to avoid tunnel player issue
     *
     * @param context Context
     * @return MediaPlayer instance
     */
    public static AudioRenderer createSilentMediaPlayer(Context context) {
        boolean result = false;

        AudioRenderer mp = null;
        try {
            PlaySoundUtil playSoundUtil = new PlaySoundUtil();
            playSoundUtil.loadSound("workaround_1min.wav");
            mp = playSoundUtil.getAudioRenderer();

            result = true;
        } catch (RuntimeException e) {
            HiLog.error(label, "createSilentMediaPlayer()" + e);
        } finally {
            if (!result && mp != null) {
                try {
                    mp.release();
                } catch (IllegalStateException e) {
                }
            }
        }

        return mp;
    }
}
